﻿using System.Collections.Generic;

namespace SSS.AlphaNN.Forms
{
    using System;
    using System.Windows.Forms;

    using Components;

    public partial class PropertiesNeuronForm : PropertiesForm
    {
        /// <summary>
        /// Идентификатор функции активации
        /// </summary>
        private int _IdFunctionActivation;

        /// <summary>
        /// Текущий нейрон
        /// </summary>
        private Neuron _sender;

        /// <summary>
        /// Конструктор принимает нейрон с данными
        /// </summary>
        /// <param name="sender"></param>
        public PropertiesNeuronForm(Neuron sender)
        {
            InitializeComponent();
            _sender = sender;        

            if (_sender.IdActivationFunction == -1)
                cmbIdFunction.SelectedIndex = 0;
            else
                cmbIdFunction.SelectedIndex = _sender.IdActivationFunction;

            ClearDataGrid();
            CreateDataGridView();
        }

        /// <summary>
        /// Чистим датагрид
        /// </summary>
        private void ClearDataGrid()
        {
            dgvNeuronProperties.Columns.Clear();
            dgvNeuronProperties.Rows.Clear();
        }

        /// <summary>
        /// Создаём датагрид
        /// </summary>
        private void CreateDataGridView()
        {
            List<object> objs = new List<object>();
            object[] table;

            dgvNeuronProperties.Columns.Add("offsetColumn", "Смещение");
            objs.Add(_sender.Offset);

            foreach (object item in _sender.ParametersForActivationFunction)
                objs.Add(item);

            switch (_IdFunctionActivation)
            {
                case 1:
                    dgvNeuronProperties.Columns.Add("step", "Порог");
                    dgvNeuronProperties.Columns.Add("beta", "Коэффициент b");
                    dgvNeuronProperties.Columns.Add("alpha", "Коэффициент a");

                    table = new object[] { 
                        objs.Count >= 1 ? objs [0] : 0,
                        objs.Count >= 2 ? objs [1] : 0.5D,
                        objs.Count >= 3 ? objs [2] : 0,
                        objs.Count >= 4 ? objs [3] : 1};
                    break;
                default:
                    dgvNeuronProperties.Columns.Add("A", "Коэффициент");

                    table = new object[] { 
                        objs.Count >= 1 ? objs [0] : 0,
                        objs.Count >= 2 ? objs [1] : 1};
                    break;
            }

            dgvNeuronProperties.Rows.Add(table);
            dgvNeuronProperties.Visible = true;
        }

        private void cmbIdFunction_SelectedIndexChanged(object sender, EventArgs e)
        {
            Dictionary<int, int> dicFunction = new Dictionary<int, int>();

            dicFunction.Add(0, 0);
            dicFunction.Add(1, 1);
            dicFunction.Add(2, 2);
            dicFunction.Add(3, 4);
            _IdFunctionActivation = dicFunction[cmbIdFunction.SelectedIndex];
                      
            ClearDataGrid();
            CreateDataGridView();
        }

        protected override void SetProperties()
        {
            DataGridViewCellCollection dataRowCells = dgvNeuronProperties.Rows[0].Cells;

            _sender.ParametersForActivationFunction.Clear();
            _sender.IdActivationFunction = cmbIdFunction.SelectedIndex;

            _sender.Offset = double.Parse(dataRowCells[0].Value.ToString());

            for (int i = 1; i < dataRowCells.Count; i++)
                _sender.ParametersForActivationFunction.Add(dataRowCells[i].Value);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SetProperties();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetProperties();
            Close();
        }

        
    }
}
